package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.MmallUser;
import com.mall.entity.vo.UserQuery;
import com.mall.service.MmallUserService;
import com.mall.utils.MD5Util;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ppmall
 * @since 2021-09-19
 */
@CrossOrigin
@RestController
@RequestMapping("/mall/user")
public class MmallUserController {

    @Autowired
    MmallUserService userService;

    @PostMapping("login")
    public R login(@RequestBody MmallUser user) {
        user = userService.getUser(user.getUsername(), user.getPassword());
        if(user != null) {
        }
        return  user != null ? R.ok().data("token", user.getId()) : R.error();
    }

    @PostMapping("logout")
    public R logout(HttpSession session) {
        session.removeAttribute("CURRENT_USER");
        return R.ok().message("登出成功");
    }

    @GetMapping("info")
    public R info(String token) {
        MmallUser user = userService.getById(token);
        System.out.println(user);
        return R.ok().data("roles", user.getRole() == 1 ? "[admin]" : "[user]")
                .data("name", user.getUsername())
                .data("avatar", "https://guli-aaa.oss-cn-hangzhou.aliyuncs.com/cat.jpeg");
    }

    @PostMapping("/users/{cur}/{limit}")
    public R getUsers(@PathVariable("cur") long cur,
                        @PathVariable("limit") long limit,
                        @RequestBody UserQuery userQuery){
        Page<MmallUser> page = new Page<>(cur, limit);
        QueryWrapper<MmallUser> wrapper = new QueryWrapper<>();

        Integer id = userQuery.getId();
        String username = userQuery.getUsername();

        if(id != null) {
            wrapper.eq("id", id);
        }

        if(!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }

        userService.page(page, wrapper);

        long total = page.getTotal();
        List<MmallUser> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @GetMapping("/user")
    public R getUserById(Integer id){
        MmallUser user = userService.getById(id);
        user.setPassword(null);
        return R.ok().data("user", user);
    }

    @PutMapping("/user")
    public R updateUserById(@RequestBody MmallUser user) {
        boolean update = userService.updateById(user);
        return update ? R.ok() : R.error();
    }

    @PostMapping("/user")
    public R saveUser(@RequestBody MmallUser user) {
        user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        boolean save = userService.save(user);
        return save ? R.ok() : R.error();
    }

    @DeleteMapping("/user/{id}")
    public R deleteUserById(@PathVariable("id") Integer id){
        boolean delete = userService.removeById(id);
        return delete ? R.ok() : R.error();
    }

}

